package com.culture.config.model;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.util.Assert;

import com.culture.config.dao.entity.UserAction;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@Accessors(chain=true)
@NoArgsConstructor
public class ActionAuthority  implements GrantedAuthority {
	private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;	
	private static final String OP_PREFIX = "OP_";	
	private UserAction userAction;
	private String actionName;
	
	public ActionAuthority(UserAction userAction) {
		Assert.notNull(userAction,"A granted authority action representation is required");
		Assert.hasText(userAction.getActionName(), "A granted authority textual representation is required");
		this.userAction = userAction;
		this.actionName = StringUtils.join(OP_PREFIX, userAction.getActionName());
	}
	
	@Override
	public String getAuthority() {	
		return this.actionName;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj instanceof ActionAuthority) {
			return this.actionName.equals(((ActionAuthority) obj).actionName);
		}

		return false;
	}

	@Override
	public int hashCode() {
		return this.actionName.hashCode();
	}

	@Override
	public String toString() {
		return this.actionName;
	}

}
